# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：chinese_workday.py
#日期：2020-10-16
#备注：中国节假日  pip install chinesecalendar  https://pypi.org/project/chinesecalendar/  
只维护到2021年
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from chinese_calendar import is_workday, is_holiday
from datetime import date
from datetime import timedelta

def is_work_day():
    april_last = date(2020, 10, 10)   # datetime.date
    print(is_workday(april_last))     # True
    print(is_holiday(april_last))     # False
    print(april_last.weekday())       # 5-星期六


# 当前日期N天前的证券交易日 t_type=t:2019-01-01、s:20190101   如果当天为非交易日，n为0
def get_trade_day(n,t_type):
    dt = date.today()
    trade_day = '20201016'
    if n < 0:
        t = -n
    else:
        t = n
    for i in range(100):
        if n<0:
            delta_day = timedelta(days=-i)
        else:
            delta_day = timedelta(days=i)
        trade_day = dt-delta_day
        if is_workday(trade_day) and trade_day.weekday()<5:       # 工作日并且不是周末
            if t ==0:
                break
            t = t -1
    if t_type=='s':
        r_trade_day = trade_day.strftime('%Y%m%d')
    else:
        r_trade_day = trade_day.strftime('%Y-%m-%d')
    print(trade_day.strftime('%Y%m%d'))
    return r_trade_day


if __name__ == '__main__':
    #is_work_day()
    get_trade_day(0,'s')